package com.epfresh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import com.epfresh.R;
import com.epfresh.adapter.CommentNewAdapter;
import com.epfresh.api.constant.AppPurchaseRouterConstant;
import com.epfresh.api.entity.*;
import com.epfresh.api.entity.RequestTag;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.entity.StatusEntity;
import com.epfresh.api.http.HttpRequest;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.T;
import com.epfresh.bean.CommentLoad;
import com.epfresh.global.BaseActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class CommentNewActivity extends BaseActivity {

	private String orderId;

	private ListView mListView;
	private ArrayList<CommentLoad.StoreListBean> newList = new ArrayList<>();

	CommentNewAdapter adapter;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (savedInstanceState != null) {
			getIntent().putExtra("orderId", savedInstanceState.getString("orderId"));
		}
		if (getIntent().getStringExtra("orderId") != null) {
			orderId = getIntent().getStringExtra("orderId");
		} else {
			T.toast("没收到id");
			finish();
		}
		initView();
		requestCommentLoad();


	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putString("orderId", orderId);
		super.onSaveInstanceState(outState);
	}

	void initView(){
		setContentView(R.layout.activity_comment_new);
		mListView = (ListView) findViewById(R.id.lv_comment_new);
		baseback.setVisibility(View.VISIBLE);
		rightbtn.setText("提交");
		toolbarTitle.setText("评价");
		rightbtn.setVisibility(View.VISIBLE);
		adapter = new CommentNewAdapter(this, newList);
		mListView.setAdapter(adapter);
		adapter.setOnItemClickListener(this);
	}
	private RequestTag requestTag = new RequestTag();

	public void requestCommentLoad() {

		RequestEntityPurchase<Map<String, Object>> entity = new RequestEntityPurchase<>();
		Map<String, Object> map = new HashMap<>();
		map.put("orderId", orderId);
		entity.setCmd(AppPurchaseRouterConstant.cmd_order_order_commentLoad);
		entity.setParameters(map);
		request(entity, requestTag, onRequestListener);

	}

	OnRequestListener<CommentLoad> onRequestListener = new OnRequestListener<CommentLoad>() {
		@Override
		public CommentLoad jsonToObj(String responseStr) {
			return new Gson().fromJson(responseStr, new TypeToken<CommentLoad>() {
			}.getType());
		}

		@Override
		public void onFail(int failCode, Object arg, Object tag) {
			hideProgressDialog();
			//updateView(tag);
			switch (failCode) {
				case HttpRequest.FAIL_NOT_FOUND:
					showExceptionView(0, null, 0);
					break;
				case HttpRequest.FAIL_NO_NET:
					showExceptionView(0, null, 0);
					break;
				case HttpRequest.FAIL_NET_FAULT:
					showExceptionView(0, null, 0);
				default:
					showExceptionView(0, null, 0);
					//showReload();
					break;
			}
		}

		@Override
		public void onResponseError(int failCode, Object arg, Object tag) {
			//updateView(tag);
			showMainView();
		}

		@Override
		public void onStart(Object tag) {

			mListView.setVisibility(View.VISIBLE);
		}

		@Override
		public void onResponse(ResponseEntity<CommentLoad> response, Object tag) {
			hideProgressDialog();
			showMainView();
			CommentLoad goodsResponse = response.getResponseElement();
			if (goodsResponse != null && goodsResponse.getStoreList().size() > 0) {
				newList.addAll(goodsResponse.getStoreList());
				adapter.notifyDataSetChanged();
			}else {
				toast("信息有误，请稍后重试");
				finish();
			}

		}
	};

int position;

	public void onClick(View v) {
		if(v.getId()==R.id.baseback) {
			finish();
			return;
		}
		if(v.getId()==R.id.rightbtn) {
			submit();
			return;
		}
		position = (Integer) v.getTag(R.id.item_key_position);
		switch (v.getId()) {
			case R.id.tv_comment_go:
				Intent evaluate = new Intent(this, CommentGoodsActivity.class);
				evaluate.putExtra("storeBean",newList.get(position));
				startActivityForResult(evaluate,0x001);
				break;
			case R.id.iv_comment_1:
			case R.id.iv_comment_2:
			case R.id.iv_comment_3:
				newList.get(position).setScore((Integer) v.getTag(R.id.item_key_status));
				adapter.notifyDataSetChanged();
				break;


		}
	}

	public void submit(){
		ArrayList<CommentLoad.StoreListBean> storeList = new ArrayList<>();

		for(int i=0;i<newList.size();i++){
			CommentLoad.StoreListBean bean = new CommentLoad.StoreListBean();
			bean.setStoreId(newList.get(i).getStoreId());
			bean.setScore(newList.get(i).getScore());

			ArrayList<CommentLoad.StoreListBean.StoreItemListBean> productList = new ArrayList<>();
			for(int j=0;j<newList.get(i).getStoreItemList().size();j++){
				CommentLoad.StoreListBean.StoreItemListBean childBean = new CommentLoad.StoreListBean.StoreItemListBean();
				childBean.setItemId(newList.get(i).getStoreItemList().get(j).getItemId());
				childBean.setContent(newList.get(i).getStoreItemList().get(j).getContent());
				childBean.setProductId(newList.get(i).getStoreItemList().get(j).getProductId());
				String affixs = new String();
				for(int m=0;newList.get(i).getStoreItemList().get(j).getImgList()!=null&&m<newList.get(i).getStoreItemList().get(j).getImgList().size();m++){
					affixs += (newList.get(i).getStoreItemList().get(j).getImgList().get(m).getId() + ",");
				}
				childBean.setAffixs(affixs);
				productList.add(childBean);
			}
			bean.setProductList(productList);
			storeList.add(bean);
		}

		RequestEntityPurchase<Map<String, Object>> entity = new RequestEntityPurchase<>();
		Map<String, Object> map = new HashMap<>();
		map.put("storeList", storeList);
		map.put("orderId", orderId);
		entity.setCmd(AppPurchaseRouterConstant.cmd_order_order_commentSave);
		entity.setParameters(map);
		request(entity, requestTag, onSubmitListener);
	}

	OnRequestListener<StatusEntity> onSubmitListener = new OnRequestListener<StatusEntity>() {
		@Override
		public StatusEntity jsonToObj(String responseStr) {
			return new Gson().fromJson(responseStr, new TypeToken<StatusEntity>() {
			}.getType());
		}

		@Override
		public void onFail(int failCode, Object arg, Object tag) {
			hideProgressDialog();
		}

		@Override
		public void onResponseError(int failCode, Object arg, Object tag) {
			hideProgressDialog();

		}

		@Override
		public void onStart(Object tag) {

			showProgressDialog();
		}

		@Override
		public void onResponse(ResponseEntity<StatusEntity> response, Object tag) {
			hideProgressDialog();
			showMainView();
			StatusEntity statusEntity = response.getResponseElement();
			if (statusEntity != null && statusEntity.isSuccess()) {
				toast("评价成功");
				setResult(RESULT_OK);
				finish();
			}else {
				toast("信息有误，请稍后重试");
				finish();
			}

		}
	};




	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK && requestCode == 0x001) {
			newList.remove(position);
			newList.add(position,(CommentLoad.StoreListBean)data.getSerializableExtra("storeBean"));
		}
	}
}
